'use strict';
const db = uniCloud.database()
const coursesCollection = db.collection('courses')

exports.main = async (event, context) => {
	const { courseId } = event
	
	try {
		if (!courseId) {
			return {
				code: -1,
				msg: '课程ID不能为空'
			}
		}
		
		// 获取课程详情
		const res = await coursesCollection.doc(courseId).get()
		
		if (!res.data || res.data.length === 0) {
			return {
				code: -1,
				msg: '课程不存在'
			}
		}
		
		return {
			code: 0,
			msg: '获取成功',
			data: res.data[0]
		}
	} catch (e) {
		console.error('获取课程详情失败', e)
		return {
			code: -1,
			msg: '获取失败',
			error: e
		}
	}
} 